<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>卡片名称</span>
      <el-button type="primary" @click="addDialog = true">添加分类</el-button>
    </div>
    <!-- 获取列表 -->
    <el-table :data="list" style="width: 100%" stripe border>
      <el-table-column prop="id" label="ID" width="150"> </el-table-column>
      <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
      <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
      <el-table-column label="操作">
        <template #default="obj">
          <el-button size="mini" @click="showEditDialog(obj.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="del(obj.row.id)">删除</el-button>
        </template>
      </el-table-column>
      <!-- 按钮 -->
    </el-table>

    <!-- 添加分类 -->
    <el-dialog title="添加分类" :before-close="cancel" :visible.sync="addDialog" width="500px">
      <el-form label-width="80px" ref="addForm" :model="addData" :rules="rules">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="addData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="addData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑对话框 -->
    <el-dialog title="编辑分类" :visible.sync="editDialog" width="500px">
      <el-form label-width="80px" ref="editForm" :model="editData" :rules="rules">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="editData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="editData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialog = false">取 消</el-button>
        <el-button type="primary" @click="editCategory">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  getCategoryAPI,
  addCategoryAPI,
  deleteCategoryAPI,
  updateCategoryAPI
} from '@/api/category'
export default {
  data() {
    return {
      // 数据先留空，等ajax请求完成，获取列表数据之后，给list赋值
      list: [],
      addDialog: false,
      editDialog: false,
      addData: {
        cate_name: '',
        cate_alias: ''
      },
      editData: {},
      rules: {
        cate_name: [
          { required: true, message: '分类名必填', trigger: 'blur' },
          { min: 1, max: 10, message: '分类名必须为1-10位', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          // {1,15} 不能写成 {1, 15}，不能有空格
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '只能是长度1~15位的字母数字组合',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 获取文档
    async getCategory() {
      // 调用API方法，获取接口数据，将得到的数据赋值给 数据项 list
      const { data: res } = await getCategoryAPI()
      if (res.code === 0) {
        this.list = res.data
      }
    },
    // 添加文章
    addCategory() {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await addCategoryAPI(this.addData)
        if (res.code === 0) {
          this.$message.success(res.message)
          // 添加完成，重置表单
          this.$refs.addForm.resetFields()
          this.addDialog = false
          // 重新获取数据内容
          this.getCategory()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 取消添加
    cancel() {
      this.$refs.addForm.resetFields()
      this.addDialog = false
    },
    // 编辑文章，弹出框
    showEditDialog(row) {
      this.editData = { ...row }
      this.editDialog = true
    },
    // 删除文章
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await deleteCategoryAPI(id)
          if (res.code === 0) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getCategory()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    editCategory() {
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await updateCategoryAPI(this.editData)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.editDialog = false
          this.getCategory()
        } else {
          this.$message.error(res.message)
        }
      })
    }
  },
  created() {
    this.getCategory()
  }
}
</script>

<style lang="less" scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
